<template>
  <div>
    <div v-if='isCountDown'>
      <span>倒计时：</span><span>{{minute}}</span> <span>:</span><span>{{second}}</span>
      <!-- <span>倒计时：</span><span>{{openTime}}</span> -->
    </div>
    <div v-if='!isCountDown'>
      <span>正在开奖...</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCountDown: true,
      minute: "00",
      second: "00",
      hour: "00",
      endTime: null,
      nowTime: null,
      isHour: false,
      timer: null
    };
  },
  props:{
    openTime:{
      type: Number
    }
  },

  watch:{
    openTime(){
      clearInterval(this.timer);
      this.countdown()
    }
  },

  methods: {
    countdown() {
      let self = this;
      let t = self.openTime;
      let key;
      this.timer = setInterval(function() {
        if (t > 0) {
          key = 0;
          self.isCountDown = true;
          let hour = Math.floor((t / 3600000) % 60);
          let min = Math.floor((t / 60000) % 60);
          let sec = Math.floor((t / 1000) % 60);
          if (hour > 0) { 
            self.isHour = true;
          } else {
            self.isHour = false;
          }
          hour = hour < 10 ? "0" + hour : hour;
          min = min < 10 ? "0" + min : min;
          sec = sec < 10 ? "0" + sec : sec;
          self.minute = min;
          self.second = sec;
          self.hour = hour;
          t = t - 1000; 
        } else {
          clearInterval(this.timer);
          self.isCountDown = false;
          if(self.$store.state.userInfo.announcement !=0 && key == 0){
            key = 1;
            let audio = new Audio();
            audio.src = "./static/video/video.mp3";
            audio.play();
          }
        }
      }, 1000);
    }
  }
};
</script>
